package com.bbm.bbmanage.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bbm.bbmanage.entity.*;
import com.bbm.bbmanage.entity.vo.LabelValueVo;
import com.bbm.bbmanage.entity.vo.PageVo;
import com.bbm.bbmanage.entity.vo.ResponseResult;
import com.bbm.bbmanage.mapper.HouseMapper;
import com.bbm.bbmanage.mapper.PropertyMapper;
import com.bbm.bbmanage.service.PropertyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * (Property)表服务实现类
 *
 * @author lionkliu
 * @since 2024-03-23 18:40:05
 */
@Service("propertyService")
public class PropertyServiceImpl extends ServiceImpl<PropertyMapper, Property> implements PropertyService {

    @Autowired
    private HouseMapper houseMapper;

    @Override
    public ResponseResult selectPage(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Property> queryWrapper = new LambdaQueryWrapper<>();
        Page<Property> page = new Page<>(pageNum, pageSize);
        Page<Property> userPage = page(page, queryWrapper);
        PageVo<Property> pageVo = new PageVo<>();
        pageVo.setResult(userPage.getRecords());
        pageVo.setTotal(userPage.getTotal());
        pageVo.setPageTotal(pageVo.getPageTotal(pageSize));
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult selectOne(Integer id) {
        return ResponseResult.okResult(getById(id));
    }

    @Override
    public ResponseResult insertOne(Property property) {
        return save(property) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.INSERT_ERROR);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        return removeById(id) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.DELETE_ERROR);
    }

    @Override
    public ResponseResult updateOne(Property property) {
        LambdaUpdateWrapper<Property> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Property::getId, property.getId());
        if (property.getName() != null) {
            updateWrapper.set(Property::getName, property.getName());
        }
        if (property.getIcon() != null) {
            updateWrapper.set(Property::getIcon, property.getIcon());
        }
        update(null, updateWrapper);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getPropertiesByHouseId(Long id) {
        return null;
    }

    @Override
    public ResponseResult search(Integer pageNum, Integer pageSize, String name) {
        LambdaQueryWrapper<Property> queryWrapper = new LambdaQueryWrapper<>();
        if (!Objects.isNull(name)) {
            queryWrapper.like(Property::getName, name);
        }
        Page<Property> page = new Page<>(pageNum, pageSize);
        page(page, queryWrapper);
        PageVo<Property> pageVo = new PageVo<>();
        pageVo.setTotal(page.getTotal());
        pageVo.setPageTotal(page.getPages());
        pageVo.setResult(page.getRecords());

        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult getPropertiesLabelValue() {
        List<LabelValueVo> collect = list().stream().map((property -> {
            LabelValueVo categoryVo = new LabelValueVo();
            categoryVo.setValue(String.valueOf(property.getId()));
            categoryVo.setLabel(property.getName());
            return categoryVo;
        })).collect(Collectors.toList());
        return ResponseResult.okResult(collect);
    }

}

